//
//  ActivityDetailView.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/16.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "ActivityDetailView.h"
@interface ActivityDetailView()<UIScrollViewDelegate>
@property(nonatomic ,strong)NSString*endDate;
@end

@implementation ActivityDetailView

{
    UIView*contenView;
    UIImageView *thinLine;


}
@synthesize mDelegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        _mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth,ScreenHeight)];
        _mainScroll.delegate = self;
        _mainScroll.userInteractionEnabled = YES;
        _mainScroll.scrollEnabled = YES;
        _mainScroll.showsVerticalScrollIndicator = FALSE;
        _mainScroll.showsHorizontalScrollIndicator = FALSE;
        [self  addSubview:_mainScroll];

        _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 210)];
        [_imageView setContentMode:UIViewContentModeScaleToFill];
        [_mainScroll addSubview:_imageView];
        
        
        contenView=[[UIView alloc]initWithFrame:CGRectMake(0,210, ScreenWidth, ScreenHeight)];
        contenView.backgroundColor=[UIColor whiteColor];
        [_mainScroll addSubview:contenView];
        
        UIImageView *_hengxian = [[UIImageView alloc]initWithFrame:CGRectMake(10,105-0.5, ScreenWidth-20, 0.5)];
        _hengxian.image=[UIImage imageNamed:@"hengxian"];
        [contenView addSubview:_hengxian];
        
        NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
        UIButton * timebtn=[[UIButton alloc]initWithFrame:CGRectMake(15, 15, 13, 13)];
        [timebtn setImage:[UIImage imageNamed:@"daojishi"] forState:UIControlStateNormal];
        [contenView addSubview:timebtn];
        
        
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(timebtn.right+3, 14, 170,14)];
        _timeLabel.font = [UIFont systemFontOfSize:14];
        _timeLabel.textAlignment=NSTextAlignmentLeft;
        [contenView addSubview:_timeLabel];
        UIButton*immediately = [[UIButton alloc]init];
        immediately.frame = CGRectMake(ScreenWidth-75,20,60, 30);
        [immediately  setTitle:@"立即报名" forState:UIControlStateNormal];
        [immediately setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        immediately.titleLabel.font =[UIFont systemFontOfSize:13];
        immediately.layer.borderColor = hongseColor.CGColor;
        [immediately.layer setCornerRadius:3.0]; //设置矩圆角半径
        [immediately.layer setBorderWidth:0.5];   //边框宽
        immediately.backgroundColor=hongseColor;
        [contenView addSubview:immediately];
        
        
        
        _actNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 55, 250, 16)];
        _actNameLabel.textColor=[UIColor blackColor];
        _actNameLabel.font=[UIFont systemFontOfSize:16];
        [contenView addSubview:_actNameLabel];
        _actTypeLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 80, 250, 14)];
        _actTypeLabel.textColor=shenhuiColor;
        _actTypeLabel.font=[UIFont systemFontOfSize:14];
        [contenView addSubview:_actTypeLabel];
        
        
       _joinCountLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-70, 65, 50, 14)];
        _joinCountLabel.textAlignment=NSTextAlignmentCenter;
        _joinCountLabel.textColor=shenhuiColor;
        _joinCountLabel.font=[UIFont systemFontOfSize:13];
        [contenView addSubview:_joinCountLabel];
       
        UILabel*immediately1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-70, 85, 50, 14)];
        immediately1.text=@"已报名";
        immediately1.textAlignment=NSTextAlignmentCenter;
        immediately1.textColor=zhonghuiColor;
        immediately1.font=[UIFont systemFontOfSize:12];
        [contenView addSubview:immediately1];
        
        
        UIButton*addressImage1 = [[UIButton alloc]init];
        addressImage1.frame = CGRectMake(20,120,20, 20);
        [addressImage1 setImage:[UIImage imageNamed:@"daohangs"] forState:UIControlStateNormal];
        [contenView addSubview:addressImage1];
        _actAddress=[[UILabel alloc]initWithFrame:CGRectMake(50, 105, ScreenWidth-40, 50)];
        _actAddress.textColor=shenhuiColor;
        _actAddress.numberOfLines=0;
        _actAddress.font=[UIFont systemFontOfSize:15];
        [contenView addSubview:_actAddress];
        
        UIButton*addressBtn = [[UIButton alloc]init];
        addressBtn.frame = CGRectMake(ScreenWidth/2,105,ScreenWidth, 50);
        [addressBtn addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        addressBtn.tag=302;
        [contenView addSubview:addressBtn];
        UIButton*addressBtn1 = [[UIButton alloc]init];
        addressBtn1.frame = CGRectMake(ScreenWidth-40,105+30,33, 33);
        [addressBtn1 setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
        [addressBtn1 addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        addressBtn1.tag=302;
        [contenView addSubview:addressBtn1];
        
        
        
        
        
        
        UIView*diseview=[[UIView alloc]initWithFrame:CGRectMake(0, 150, ScreenWidth, 10)];
        diseview.backgroundColor=zuiqianhuiColor;
        diseview.layer.borderColor = qianhuiColor.CGColor;
        [diseview.layer setBorderWidth:0.40];   //边
        [contenView addSubview:diseview];
        
        
        UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0,160, ScreenWidth, 140)];
        view1.backgroundColor=[UIColor whiteColor];
        [contenView addSubview:view1];
        UIView*view2=[[UIView alloc]initWithFrame:CGRectMake(0, 300, ScreenWidth, 10)];
        view2.backgroundColor=zuiqianhuiColor;
        view2.layer.borderColor = qianhuiColor.CGColor;
        [view2.layer setBorderWidth:0.40];   //边
        [contenView addSubview:view2];
        
        
        
        _companyName=[[UILabel alloc]initWithFrame:CGRectMake(90, 20, ScreenWidth-40, 16)];
        _companyName.textColor=[UIColor blackColor];
        _companyName.font=[UIFont systemFontOfSize:16];
        [view1 addSubview:_companyName];
        
        
        
        UILabel*certification=[[UILabel alloc]initWithFrame:CGRectMake(90, 45, 50, 14)];
        certification.text=@"物管认证";
        certification.textAlignment=NSTextAlignmentCenter;
        certification.textColor=[UIColor whiteColor];
        certification.font=[UIFont systemFontOfSize:11];
        certification.backgroundColor=luseColor;
        [view1 addSubview:certification];
        
        UILabel*certification1=[[UILabel alloc]initWithFrame:CGRectMake(150, 45, 50, 14)];
        certification1.text=@"经营认证";
        certification1.textAlignment=NSTextAlignmentCenter;
        certification1.textColor=[UIColor whiteColor];
        certification1.font=[UIFont systemFontOfSize:11];
        certification1.backgroundColor=luseColor;
        [view1 addSubview:certification1];
        _companyBaseInfo=[[UILabel alloc]initWithFrame:CGRectMake(90, certification1.bottom, ScreenWidth-90, 30)];
        [view1 addSubview:_companyBaseInfo];
        
        
        UILabel*line1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2, 100, 0.40, 30)];
        line1.backgroundColor=qianhuiColor;
        [view1 addSubview:line1];
        _companyImgUrlFull = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 140/2, 140/2)];
        _companyImgUrlFull.layer.masksToBounds = YES;
        _companyImgUrlFull.backgroundColor=zuiqianhuiColor;
        _companyImgUrlFull.layer.cornerRadius = 140/2/2;
        _companyImgUrlFull.layer.borderColor = zuiqianhuiColor.CGColor;
        _companyImgUrlFull.layer.borderWidth = 2;
        _companyImgUrlFull.contentMode = UIViewContentModeScaleToFill;
        [view1 addSubview:_companyImgUrlFull];
        
        
        UIButton*telImage = [[UIButton alloc]init];
        telImage.frame = CGRectMake(50,105,20, 20);
        [telImage setImage:[UIImage imageNamed:@"dianhuasss"] forState:UIControlStateNormal];
        [view1 addSubview:telImage];
        UILabel*tel=[[UILabel alloc]initWithFrame:CGRectMake(60, 90, ScreenWidth/2-70, 50)];
        tel.text=[NSString stringWithFormat:@"%@", @"电话联系"];
        tel.textColor=shenhuiColor;
        tel.numberOfLines=0;
        tel.textAlignment=NSTextAlignmentCenter;
        tel.font=[UIFont systemFontOfSize:15];
        [view1 addSubview:tel];
       
        UIButton*Btn = [[UIButton alloc]init];
        Btn.frame = CGRectMake(0,90,ScreenWidth/2, 50);
        [Btn addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        Btn.tag=301;
        [view1 addSubview:Btn];
        
        
        UIButton*addressImage = [[UIButton alloc]init];
        addressImage.frame = CGRectMake(ScreenWidth/2+40,105,20, 20);
        [addressImage setImage:[UIImage imageNamed:@"daohangsss"] forState:UIControlStateNormal];
        [view1 addSubview:addressImage];
        UILabel*address=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2, 90, ScreenWidth/2, 50)];
        address.text=[NSString stringWithFormat:@"%@", @"    去那里"];
        address.textColor=shenhuiColor;
        address.textAlignment=NSTextAlignmentCenter;
        address.font=[UIFont systemFontOfSize:15];
        [view1 addSubview:address];
       
        
        UIButton*Btn1 = [[UIButton alloc]init];
        Btn1.frame = CGRectMake(ScreenWidth/2,90,ScreenWidth/2, 50);
        [Btn1 addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        Btn1.tag=302;
        [view1 addSubview:Btn1];
        
        UILabel*EventDetails=[[UILabel alloc]initWithFrame:CGRectMake(15, view2.bottom, 100, 45)];
        EventDetails.text=@"活动详情";
        EventDetails.textColor=[UIColor blackColor];
        EventDetails.font=[UIFont systemFontOfSize:15];
        [contenView addSubview:EventDetails];
        thinLine = [[UIImageView alloc]initWithFrame:CGRectMake(10,EventDetails.bottom, ScreenWidth-20, 0.5)];
        thinLine.image=[UIImage imageNamed:@"hengxian"];
        [contenView addSubview:thinLine];
        
        
    }
    return self;
}

-(void)setModel:(ActivityDetailModel *)model{
    
        self.endDate=[model.Dict objectForKey:@"endDate"];
    
     [_imageView setImageWithURL:[NSURL URLWithString:[model.Dict objectForKey:@"imageUrlFull"]] placeholderImage:[UIImage imageNamed:@"changfangxing"]];
        _actNameLabel.text=[model.Dict objectForKey:@"actName"];
        _actTypeLabel.text=[NSString stringWithFormat:@"%@ %@",  @"活动类型",[model.Dict objectForKey:@"actType"]];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ %@",  @"活动类型",[model.Dict objectForKey:@"actType"]]];
    
            [AttributedStr addAttribute:NSFontAttributeName
    
                                  value:[UIFont systemFontOfSize:14]
    
                                  range:NSMakeRange(0, 4)];
    
            [AttributedStr addAttribute:NSForegroundColorAttributeName
    
                                  value:zhonghuiColor
    
                                  range:NSMakeRange(0, 4)];
    
    
            _actTypeLabel.attributedText = AttributedStr;
    
    
          _joinCountLabel.text=[ NSString stringWithFormat:@"%@", [model.Dict objectForKey:@"joinCount"]];
    
        _actAddress.text=[NSString stringWithFormat:@"%@%@", [model.Dict objectForKey:@"actRegion"], [model.Dict objectForKey:@"actAddress"]];
          _companyName.text=[[model.Dict objectForKey:@"companyBaseInfo"] objectForKey:@"companyName"];
    
            _companyBaseInfo.text=[NSString stringWithFormat:@"主营 %@  ",  [[model.Dict objectForKey:@"companyBaseInfo"] objectForKey:@"businessType"]];
            _companyBaseInfo.textColor=shenhuiColor;
            _companyBaseInfo.font=[UIFont systemFontOfSize:13];
            NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"主营 %@  ",  [[model.Dict objectForKey:@"companyBaseInfo"] objectForKey:@"businessType"]]];
    
            [AttributedStr1 addAttribute:NSFontAttributeName
    
                                   value:[UIFont systemFontOfSize:13.0]
    
                                   range:NSMakeRange(0, 3)];
    
            [AttributedStr1 addAttribute:NSForegroundColorAttributeName
    
                                   value:zhonghuiColor
    
                                   range:NSMakeRange(0, 3)];
    
    
            _companyBaseInfo.attributedText = AttributedStr1;
    
          [_companyImgUrlFull setImageWithURL:[NSURL URLWithString:[[model.Dict objectForKey:@"companyBaseInfo"]objectForKey:@"companyImgUrlFull"]] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    
    
    
            NSString *s12 = [NSString stringWithFormat:@"%@", [model.Dict objectForKey:@"actDetail"]];
            UIFont *font12 = [UIFont systemFontOfSize:15];
            CGSize size12 = CGSizeMake(ScreenWidth-20, 2000);
            CGSize labelsize12 = [s12 sizeWithFont:font12 constrainedToSize:size12 lineBreakMode:NSLineBreakByCharWrapping];
    
           _detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, thinLine.bottom+25, ScreenWidth-20, labelsize12.height)];
            _detailLabel.text=[NSString stringWithFormat:@"%@", [model.Dict objectForKey:@"actDetail"]];
            _detailLabel.textColor=shenhuiColor;
            _detailLabel.numberOfLines=0;
            _detailLabel.font=[UIFont systemFontOfSize:15];
            [contenView addSubview:_detailLabel];
    
            contenView.frame=CGRectMake(0, 210, ScreenWidth, _detailLabel.bottom);
            [_mainScroll setContentSize:CGSizeMake(ScreenWidth, contenView.bottom+64)];
    
    
    
}
- (void)TouchUpInside:(UIButton *)button{
    
    
    if (button.tag==301) {
        
         [mDelegate ACETelPrompt:@""];
 
        
    }
    if (button.tag==302) {
        
        [mDelegate address:@""];

    }
    
   }


- (void)timerFireMethod:(NSTimer *)timer

{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    
    NSString* string = self.endDate;
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate= [dateFormatter1 dateFromString:string];
    
    
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *d = [calendar components:unitFlags fromDate: [NSDate date] toDate:destDate options:0];//计算时间差
    
    
    NSString* string2 = self.endDate;
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate2= [dateFormatter2 dateFromString:string2];
    NSTimeInterval  timeInterval2 = [destDate2 timeIntervalSinceDate:[NSDate date]];
    
    
    if ([d day] <=0) {
        
        
        if ([d month] <=0) {
            _timeLabel.text = [NSString stringWithFormat:@"剩%@天%ld小时   %ld分%ld秒",@"0", (long)[d hour], (long)[d minute], (long)[d second]];
            
            _timeLabel.textColor = [UIColor colorWithRed:204.0/255.0 green:3.0/255.0 blue:2.0/255.0 alpha:1.0];
            
            NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"剩%@天%ld小时   %ld分%ld秒",@"0", (long)[d hour], (long)[d minute], (long)[d second]]];
            
            [AttributedStr addAttribute:NSFontAttributeName
             
                                  value:[UIFont systemFontOfSize:14]
             
                                  range:NSMakeRange(0, 9)];
            
            [AttributedStr addAttribute:NSForegroundColorAttributeName
             
                                  value:shenhuiColor
             
                                  range:NSMakeRange(0, 9)];
            
            
            _timeLabel.attributedText = AttributedStr;
        }
        
        if ([d second] <0) {
            _timeLabel.text=@"已结束";
            
        }
        
        
        
        
    }else{
        
        if ([d month] <=0) {
            _timeLabel.text = [NSString stringWithFormat:@"剩%ld天%ld小时   %ld分%ld秒", (long)[d day], (long)[d hour], (long)[d minute], (long)[d second]];
        }
        
        if ([d second] <0) {
            _timeLabel.text=self.endDate;
            
        }
        
        _timeLabel.textColor = [UIColor colorWithRed:204.0/255.0 green:3.0/255.0 blue:2.0/255.0 alpha:1.0];
        
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"剩%.0f天%ld小时   %ld分%ld秒",timeInterval2/60/60/24-1, (long)[d hour], (long)[d minute], (long)[d second]]];
        
        [AttributedStr addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:14]
         
                              range:NSMakeRange(0, 9)];
        
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:shenhuiColor
         
                              range:NSMakeRange(0, 9)];
        
        
        _timeLabel.attributedText = AttributedStr;
        
    }
    
}

@end
